/*!
 Temelia - Binary tree samples.

 Copyright (C) 2008 Ceata (http://ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/samples.h"
#include <temelia/binary_tree.h>

#ifdef _WIN32

#else
#include <sys/socket.h>
#endif

static void sample1()
{
	binary_tree_t tree;

#ifdef _WIN32
	int *adr;
#else
	struct sockaddr *adr;
#endif

	PRINT("\n\n Sample 1 started \n\n");

#ifdef _WIN32
	adr = (int *) malloc(sizeof(int));
#else
	adr = (struct sockaddr *) malloc(sizeof(struct sockaddr));
#endif

	tree = binary_tree_new(adr);

	PRINT("Leaf %d\n", binary_tree_is_leaf(tree));
	PRINT("Empty %d\n", binary_tree_is_empty(tree));
	PRINT("Key %p, inserted key %p\n", binary_tree_get_key(tree), adr);

	binary_tree_delete(tree);
	free(adr);

	PRINT("\n\n Sample 1 ended \n\n");
}

static void sample2()
{
	int *x, *y, *z;
	binary_tree_t parent, right, left;

	PRINT("\n\n Sample 2 started \n\n");

	x = (int *) malloc(sizeof(int));
	*x = 1;
	y = (int *) malloc(sizeof(int));
	*y = 2;
	z = (int *) malloc(sizeof(int));
	*z = 3;

	parent = binary_tree_new(x);
	left = binary_tree_new(y);
	right = binary_tree_new(z);
	binary_tree_join(parent, left, right);

	binary_tree_show_indented(parent, int_level_handler, NULL);

	binary_tree_delete(parent);
	free(x);
	free(y);
	free(z);
	PRINT("\n\n Sample 2 ended \n\n");
}

static void sample3()
{
	int i, N = 50;
	binary_tree_t tree, root, add;

	PRINT("\n\n Sample 3 started \n\n");

	root = binary_tree_new(int_new(rand() % 1000));
	tree = root;

	for (i = 1; i < N; i++)
	{
		add = binary_tree_new(int_new(rand() % 1000));
		if (rand() % 2)
			// Add left child and step into it.
			binary_tree_set_left_child(tree, add);
		else
			// Add right child and step into it.
			binary_tree_set_right_child(tree, add);
		tree = add;
	}

	PRINT("\nIndent print : \n");
	binary_tree_show_indented(root, int_level_handler, NULL);

	PRINT("\nPreorder : \n");
	binary_tree_preorder(root, int_handler, NULL);

	PRINT("\nPostorder : \n");
	binary_tree_postorder(root, int_handler, NULL);

	PRINT("\nInorder : \n");
	binary_tree_inorder(root, int_handler, NULL);

	PRINT("\nReverse Inorder : \n");
	binary_tree_reverse_inorder(root, int_handler, NULL);

	binary_tree_reverse_inorder(root, int_delete, NULL);
	binary_tree_delete(root);

	PRINT("\n\n Sample 3 ended \n\n");
}

void run_binary_tree_samples()
{
	sample1();
	sample2();
	sample3();
}
